package com.pp.ent.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;

import java.sql.SQLException;

/**
 * Created by dengqu on 2017/2/24.
 */

public abstract class DbHelper
        extends OrmLiteSqliteOpenHelper {
    private String dbName;
    private String dbOpenAccountAction;
    private String dbOpenAccountDbName;

    public DbHelper(Context context, String dbName, int dbVersion) {
        super(context, dbName, null, dbVersion);
        this.dbName = dbName;
        this.logger.info("DbHelper constructor");
    }

    public String getDbName() {
        return this.dbName;
    }

    public void setDBOpenAccount(String action, String dbName) {
        this.dbOpenAccountAction = action;
        this.dbOpenAccountDbName = dbName;
    }

    public String getDbOpenAccountAction() {
        return this.dbOpenAccountAction;
    }

    public String getDbOpenAccountDbName() {
        return this.dbOpenAccountDbName;
    }

    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        this.logger.info("DbHelper onCreate, name = " + this.dbName);
        try {
            onDbCreate(database, connectionSource);
        } catch (SQLException e) {
            this.logger.error("DbHelper onCreate error", e);
        }
    }

    protected abstract void onDbCreate(SQLiteDatabase paramSQLiteDatabase, ConnectionSource paramConnectionSource)
            throws SQLException;

    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        this.logger.info("DbHelper onUpdate old " + oldVersion + " new " + newVersion);
        try {
            onDbUpgrade(database, connectionSource, oldVersion, newVersion);
        } catch (SQLException e) {
            this.logger.error("DbHelper onUpgrade error", e);
        }
    }

    protected abstract void onDbUpgrade(SQLiteDatabase paramSQLiteDatabase, ConnectionSource paramConnectionSource, int paramInt1, int paramInt2)
            throws SQLException;
}

